﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity
{
    public class SMDBContext:DbContext
    {

        public SMDBContext(DbContextOptions<SMDBContext> opts):base(opts)
        {
        }
        /// <summary>
        /// 账户
        /// </summary>
        public DbSet<EntityAccount> EntityAccount { get; set; }
        /// <summary>
        /// 用户信息
        /// </summary>
        public DbSet<EntityUser> EntityUserInfo { get; set; }
        /// <summary>
        /// 报名表
        /// </summary>
        public DbSet<EntityEntryForm> EntityEntryForm { get; set; }

        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    optionsBuilder.UseMySql(_connectionString, new MySqlServerVersion(new Version(9, 2, 0)));
        //}
        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    modelBuilder.UseCollation("utf8mb4").HasCharSet("utf8mb4_german2_ci");
        //}
        
        /// <summary>
        /// 数据模型（表）构建函数
        /// 1：设计表的一些字符集
        /// 2：设定数据库模型的主外键
        /// 3：添加默认数据
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            EntityUser user = new EntityUser();
            user.FristName = "123123";
            user.LastName = "3453";


            modelBuilder.Entity<EntityUser>().HasData([

                new EntityUser{
                    Name="11",
                    FristName="zhangsan",
                    LastName="san",
                    Email="4444sdfsdf@sdf.cvf"
                },

                ]);

        }
    }
    
    /// <summary>
    /// 告诉负责EF框架，如何创建连接数据上下文
    /// </summary>
    public class ApiDbContextFactory:IDesignTimeDbContextFactory<SMDBContext>
    {
    public SMDBContext CreateDbContext(string[] args)
        {
            var optionBuilder = new DbContextOptionsBuilder<SMDBContext>();
            optionBuilder.UseMySql(
                "server=localhost;userid=root;password=123456;database=AIYDB",
                new MySqlServerVersion(new Version(8, 0, 41))
                );

            return new SMDBContext(optionBuilder.Options);
        }
    }
}
